package com.ligong.mapper;

import com.ligong.domain.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class UserMapperImpl implements UserMapper{

    public static void main(String[] args) {
        try {
            new UserMapperImpl().selectOneById(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public Users selectOneById(int id) throws Exception {

        //也需要执行sql语句


        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession();  //Connection

        Users user=sqlSession.selectOne("users.selectOneById",id);

        System.out.println(user.getUserName());

        //sqlSession.commit();  //手动提交，才能看到更新的结果

        sqlSession.close();


        return null;
    }
}
